package com.busibuilding.administrate.area.action;

import java.util.List;

import org.displaytag.tags.TableTagParameters;

import com.busibuilding.administrate.area.service.CityService;
import com.busibuilding.administrate.area.vo.CityCVO;
import com.busibuilding.common.action.CommonActionImpl;
import com.busibuilding.utils.StringUtil;
/**
 * Class Name: CityAction.java
 * Description: 
 * @author Lin Yanjun
 * @version 1.0
 * @since 1.0
 * Date: 2010-2-25
 */
public class CityAction extends CommonActionImpl {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4456035680912126525L;
	
	private boolean showAdd;
	private boolean showUpdate;
	private boolean showTable;
	
	private CityForm cityForm = new CityForm(); 
	
	private int page;
	private int currentPage;
	
	private CityService service;

	public String search() throws Exception{
		CityCVO cityCVO = new CityCVO();
		if(StringUtil.isNotEmpty(cityForm.getProvinceCd())){
			cityCVO.setProvinceCd(cityForm.getProvinceCd());
		}
		if(StringUtil.isNotEmpty(cityForm.getCityCd())){
			cityCVO.setCityCd(cityForm.getCityCd());
		}
		if(StringUtil.isNotEmpty(cityForm.getCityName())){
			cityCVO.setCityName(cityForm.getCityName());
		}
		if(StringUtil.isNotEmpty(cityForm.getDefunctInd())){
			cityCVO.setDefunctInd(cityForm.getDefunctInd());
		}else{
			cityCVO.setDefunctInd("N");
		}
		cityCVO.setMinIndex((getPage()-1)*10);
		cityCVO.setMaxIndex(getPage()*10);
		int listSize = service.getRecordsCount(cityCVO);
		List<CityCVO> resultList = service.searchCity(cityCVO);
		this.getRequest().setAttribute("cityListSize", listSize);
		this.getRequest().setAttribute("cityDisplayList", resultList);
		cityForm.setProvinceCd(cityCVO.getProvinceCd());
		showTable = true;
		return INPUT;
	}
	
	public String del() throws Exception{
		CityCVO cityCVO = new CityCVO();
		cityCVO.setCityId(cityForm.getCityId());
		cityCVO.setDefunctInd(YES);
		getAdminCommonInformationForUpdate(cityCVO);
		int code = service.deleteCity(cityCVO);
		if(code==SQLMAP_COMMON_RESULT_ERROR){
			txnFailure(TRANS_SUCCESSFUL_MSG);
		}else{
			txnSuccessful(TRANS_SUCCESSFUL_MSG);
		}
		return "toSearchCity";
	}
	
	public String inputCreate() throws Exception{
		showAdd = true;
		showUpdate = false;
		showTable = false;
		return INPUT;
	}
	
	public String createCity() throws Exception{
		if (hasErrors()) {
			return INPUT;
		}
		CityCVO cityCVO = new CityCVO();
		cityCVO.setCityCd(cityForm.getCityCd());
		List<CityCVO> resultList = service.searchCity(cityCVO);
		if(resultList.size()>0){
			addActionError(getText("general.message.rectifyInRed"));
			addFieldError("cityForm.cityCd", getText(
					"error.admin.sysmgmt.area.existCityCode",
					new String[] { cityForm.getCityCd() }));
			cityForm.setCityCd(EMPUT_STRING);
			return INPUT;
		}
		cityCVO.setCityName(cityForm.getCityName());
		cityCVO.setProvinceCd(cityForm.getProvinceCd());
		getAdminCommonInformationForCreate(cityCVO);
		service.createCity(cityCVO);
		return "toSearchCity";
	}
	
	public String inputUpdate() throws Exception{
		CityCVO cityCVO = new CityCVO();
		cityCVO.setCityId(cityForm.getCityId());
		cityCVO = (CityCVO) service.getCity(cityCVO);
		cityForm.setCityCd(cityCVO.getCityCd());
		cityForm.setCityName(cityCVO.getCityName());
		cityForm.setProvinceCd(cityCVO.getProvinceCd());
		showAdd = false;
		showUpdate = true;
		showTable = false;
		return INPUT;
	}
	
	public String updateCity() throws Exception{
		if (hasErrors()) {
			return INPUT;
		}
		CityCVO tempCityCVO = new CityCVO();
		tempCityCVO.setCityCd(cityForm.getCityCd());
		tempCityCVO.setProvinceCd(cityForm.getProvinceCd());
		List<CityCVO> resultList = service.searchCity(tempCityCVO);
		if(resultList.size()>1){
			addActionError(getText("general.message.rectifyInRed"));
			addFieldError("cityForm.cityCd", getText(
					"error.admin.sysmgmt.area.existCityCode",
					new String[] { cityForm.getCityCd() }));
			cityForm.setCityCd(EMPUT_STRING);
			return INPUT;
		}
		CityCVO cityCVO = new CityCVO();
		cityCVO.setCityId(cityForm.getCityId());
		cityCVO = (CityCVO) service.getCity(cityCVO);
		cityCVO.setCityCd(cityForm.getCityCd());
		cityCVO.setCityName(cityForm.getCityName());
		getAdminCommonInformationForUpdate(cityCVO);
		cityCVO.setVersion(cityCVO.getVersion()+1);
		service.updateCity(cityCVO);
		return "toSearchCity";
	}
	
	public boolean isShowAdd() {
		return showAdd;
	}

	public void setShowAdd(boolean showAdd) {
		this.showAdd = showAdd;
	}

	public boolean isShowUpdate() {
		return showUpdate;
	}

	public void setShowUpdate(boolean showUpdate) {
		this.showUpdate = showUpdate;
	}

	public boolean isShowTable() {
		return showTable;
	}

	public void setShowTable(boolean showTable) {
		this.showTable = showTable;
	}

	public CityForm getCityForm() {
		return cityForm;
	}

	public void setCityForm(CityForm cityForm) {
		this.cityForm = cityForm;
	}

	public int getPage() {
		if (getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE) == null || "".equals(getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE))) {
        	page = currentPage;
        }else{
        	page = Integer.parseInt(getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE));
        }
		
		if (page == 0){
			page = DEFAULT_PAGE;
		}
		
		return page;
	}

	public void setPage(int page) {
		if (page > 0){
			this.page = page;
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setService(CityService service) {
		this.service = service;
	}
}
